<?php

/**
 * Description of String
 *
 * @author Admin
 */
class String
{
	protected static $_tr = array(
        'ru' => array(
            "А"=>"a","Б"=>"b","В"=>"v","Г"=>"g",
            "Д"=>"d","Е"=>"e","Ж"=>"j","З"=>"z","И"=>"i","І"=>"i","Ї"=>'i',
            "Й"=>"y","К"=>"k","Л"=>"l","М"=>"m","Н"=>"n",
            "О"=>"o","П"=>"p","Р"=>"r","С"=>"s","Т"=>"t","Ё"=>"E",
            "У"=>"u","Ф"=>"f","Х"=>"h","Ц"=>"ts","Ч"=>"ch",
            "Ш"=>"sh","Щ"=>"sch","Ъ"=>"","Ы"=>"y","Ь"=>"",
            "Э"=>"e","Є"=>"e","Ю"=>"yu","Я"=>"ya","а"=>"a","б"=>"b",
            "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"j",
            "з"=>"z","и"=>"i","і"=>"i","ї"=>'i',"й"=>"y","к"=>"k","л"=>"l",
            "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
            "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h","ё"=>"e",
            "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y",
            "ы"=>"y","ь"=>"","э"=>"e","є"=>"e","ю"=>"yu","я"=>"ya",
            " "=> "-", "."=> "", ","=> "", "/"=> "_",
            "«"=>'',"»"=>'',"!"=>"","@"=>"_at_","#"=>"_","$"=>"","%"=>"","^"=>"",
            "&"=>"","*"=>"","("=>"",")"=>"","|"=>"","\\"=>"_",":"=>"_",";"=>"_","'"=>"",
            '"'=>"","~"=>"-","№"=>"","?"=>"","+"=>"",","=>"",
        ),
        'uk' => array(
            "А"=>"a","Б"=>"b","В"=>"v","Г"=>"g",
            "Д"=>"d","Е"=>"e","Ж"=>"j","З"=>"z","И"=>"y","І"=>"i","Ї"=>'i',
            "Й"=>"j","К"=>"k","Л"=>"l","М"=>"m","Н"=>"n",
            "О"=>"o","П"=>"p","Р"=>"r","С"=>"s","Т"=>"t",
            "У"=>"u","Ф"=>"f","Х"=>"h","Ц"=>"ts","Ч"=>"ch",
            "Ш"=>"sh","Щ"=>"sch","Ъ"=>"","Ы"=>"y","Ь"=>"",
            "Э"=>"e", "Є"=>"e","Ю"=>"yu","Я"=>"ya","а"=>"a","б"=>"b",
            "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"j",
            "з"=>"z","и"=>"y","і"=>"i","ї"=>'i',"й"=>"y","к"=>"k","л"=>"l",
            "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
            "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
            "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y",
            "ы"=>"y","ь"=>"","э"=>"e","є"=>"e","ю"=>"yu","я"=>"ya",
            " "=> "-", "."=> "", ","=> "", "/"=> "_",
            "«"=>'',"»"=>'',"!"=>"","@"=>"_at_","#"=>"_","$"=>"","%"=>"","^"=>"",
            "&"=>"","*"=>"","("=>"",")"=>"","|"=>"","\\"=>"_",":"=>"_",";"=>"_","'"=>"",
            '"'=>"","~"=>"-","№"=>"","?"=>"","+"=>"",","=>"",
        ),
        'en' => array(
            " "=> "-", "."=> "", ","=> "", "/"=> "_",
            "«"=>'',"»"=>'',"!"=>"","@"=>"_at_","#"=>"_","$"=>"","%"=>"","^"=>"",
            "&"=>"","*"=>"","("=>"",")"=>"","|"=>"","\\"=>"_",":"=>"_",";"=>"_","'"=>"",
            '"'=>"","~"=>"-","№"=>"","?"=>"","+"=>"",","=>"",
        ),
        'file' => array(
            "А"=>"a","Б"=>"b","В"=>"v","Г"=>"g",
            "Д"=>"d","Е"=>"e","Ж"=>"j","З"=>"z","И"=>"i","І"=>"i","Ї"=>'i',
            "Й"=>"y","К"=>"k","Л"=>"l","М"=>"m","Н"=>"n",
            "О"=>"o","П"=>"p","Р"=>"r","С"=>"s","Т"=>"t",
            "У"=>"u","Ф"=>"f","Х"=>"h","Ц"=>"ts","Ч"=>"ch",
            "Ш"=>"sh","Щ"=>"sch","Ъ"=>"","Ы"=>"y","Ь"=>"",
            "Э"=>"e","Є"=>"e","Ю"=>"yu","Я"=>"ya","а"=>"a","б"=>"b",
            "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"j",
            "з"=>"z","и"=>"i","і"=>"i","ї"=>'i',"й"=>"y","к"=>"k","л"=>"l",
            "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
            "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
            "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y",
            "ы"=>"y","ь"=>"","э"=>"e","є"=>"e","ю"=>"yu","я"=>"ya",
            " "=> "-","«"=>'',"»"=>'',"!"=>"","@"=>"_at_","#"=>"_","$"=>"","%"=>"","^"=>"",
            "&"=>"","*"=>"","("=>"",")"=>"","|"=>"","\\"=>"/",":"=>"_",";"=>"_","'"=>"",
            '"'=>"","~"=>"-","№"=>"","?"=>"","+"=>"",","=> "",
        ),
	);

	public static function translit($text, $lang = 'ru', $isUrl = false)
	{
		$tr = isset(self::$_tr[$lang]) ? self::$_tr[$lang] : current(self::$_tr);

		$text = strtr($text,$tr);

		if($isUrl)
		{
			$text = preg_replace("|[^a-z\d\s-_,.\(\)]|i", "", $text);
		}

		$text = mb_eregi_replace("php", "", $text); //убираем сочетания php
		if($lang != 'file')
			$text = mb_eregi_replace("[\s\.]", "-", $text); // пробелы и точки заменяем на дефизы
		$text = mb_eregi_replace("-{2,}", "-", $text); // 2 и больше дефиза заменяем на 1
		$text = mb_eregi_replace("[^a-z0-9\-\.]+", "", $text); // убираем все, кроме букв и цифр

		return $text;

//		return mb_convert_encoding($text, 'US-ASCII//TRANSLIT', "UTF-8");
//		return iconv("UTF-8", 'US-ASCII//TRANSLIT', $text);
	}

	/**
	 * В тэгах вырезаются запрещенные атрибуты
	 *
	 * @param string $string
	 * @param string | array $allowtags
	 * @param string | array $allowattributes
	 * @return string
	 */
	public static function stripTagsAttributes( $string, $allowtags=NULL, $allowattributes=NULL )
	{
		$string = strip_tags($string,$allowtags);
		if (!is_null($allowattributes)) {
			if(!is_array($allowattributes))
				$allowattributes = explode(",",$allowattributes);
			if(is_array($allowattributes))
				$allowattributes = implode(")(?<!",$allowattributes);
			if (strlen($allowattributes) > 0)
				$allowattributes = "(?<!".$allowattributes.")";
			$string = preg_replace_callback("/<[^>]*>/i",create_function(
				'$matches',
				'return preg_replace("/ [^ =]*'.$allowattributes.'=(\"[^\"]*\"|\'[^\']*\')/i", "", $matches[0]);'
			),$string);
		}

		return $string;
	}

    public static function substr($str, $chars = 150)
    {
        if(!$str || strlen($str) < $chars)
            return $str;

        $pos = strpos($str, ' ', $chars);
        if($pos !== false)
            $str = substr($str, 0, $pos);

        return $str;
    }

    public static function declination($n, $forms) {
        if($n == 0)
            return $forms[3];
        return $n%10==1&&$n%100!=11?$forms[0]:($n%10>=2&&$n%10<=4&&($n%100<10||$n%100>=20)?$forms[1]:$forms[2]);
    }

	public static function correctString ($string)
	{
		$search = array(
			"й","ц","у","к","е","н","г","ш","щ","з","х","ъ",
			"ф","ы","і","в","а","п","р","о","л","д","ж","э",
			"я","ч","с","м","и","т","ь","б","ю"
		);
		$replace = array(
			"q","w","e","r","t","y","u","i","o","p","[","]",
			"a","s","s","d","f","g","h","j","k","l",";","'",
			"z","x","c","v","b","n","m",",","."
		);

		$arep = array(
			'й'=>'q','ц'=>'w','у'=>'e','к'=>'r','е'=>'t','н'=>'y','г'=>'u','ш'=>'i','щ'=>'o','з'=>'p','х'=>'','ъ'=>'',
			'ф'=>'a','ы'=>'s','в'=>'d','а'=>'f','п'=>'g','р'=>'h','о'=>'j','л'=>'k','д'=>'l','ж'=>'','э'=>'',
			'я'=>'z','ч'=>'x','с'=>'c','м'=>'v','и'=>'b','т'=>'n','ь'=>'m','б'=>'','ю'=>''
		);

		return strtr($string, $arep);
	}

}
